"use strict"

window.resume = {
    toolBarHide: document.body.clientWidth < 760,
    home: "resume",
    mode: "pc"
};

(function() {
    var navLi = document.querySelectorAll("nav li");
    var toolBar = document.querySelector(".tool-bar");
    var navForm = document.querySelector("nav form");
    var navFormInput = document.querySelector("nav form input");

    function removeClass(ele, cls) {
        if (hasClass(ele, cls)) {
            var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
            ele.className = ele.className.replace(reg, " ");
        }
    }

    function hasClass(ele, cls) {
        return ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
    }

    function mobileInit() {
        window.resume.mode = "mobile";  /* 改变状态为移动端 */
        window.resume.toolBarHide = true;  /* 改变菜单状态为隐藏，这里由css文件操作，js只定义状态 */
        var navLi = document.querySelectorAll("nav li");
        var toolBar = document.querySelector(".tool-bar");
        /* 点击菜单按钮 */
        var once;
        for (once = 0; once < navLi.length; once++) {
            navLi[once].style.display = "none";
        }
        navForm.style.display = "none";
    }

    function pcInit() {
        window.resume.mode = "pc";
        window.resume.toolBarHide = false;
        navForm.style.width = "174px";  /* 搜索框长度 */
        var navLi = document.querySelectorAll("nav li");
        var once;
        for (once = 0; once < navLi.length; once++) {
            navLi[once].style.display = "block";
        }
        navForm.style.display = "block";
        window.resume.toolBarHide = false;
    }

    function ctrlMobile(sta) {
        var staOnce;
        if (sta == "hide") {
            for (staOnce = 0; staOnce < navLi.length; staOnce++) {
                navLi[staOnce].style.display = "none";
            }
            navForm.style.display = "none";
            window.resume.toolBarHide = true;
        }
    }

    document.addEventListener("DOMContentLoaded", function(event) {
        /* 初始化菜单按钮 */
        var once;
        toolBar.addEventListener("click", function(event) {
            if (window.resume.toolBarHide) {
                /* 隐藏的情况 */
                for (once = 0; once < navLi.length; once++) {
                    navLi[once].style.display = "block";
                }
                navForm.style.display = "block";
                navForm.style.width = "90%";
                window.resume.toolBarHide = false;
            } else {
                /* 显示的情况 */
                for (once = 0; once < navLi.length; once++) {
                    navLi[once].style.display = "none";
                }
                navForm.style.display = "none";
                navForm.style.width = "174px";
                window.resume.toolBarHide = true;
            }
        });

        if (document.body.clientWidth <= 760 && window.resume.mode != "mobile") {
            mobileInit();  /* 移动模式常规操作 */
        } else if (document.body.clientWidth > 760 && window.resume.mode == "mobile") {
            pcInit();
        }
/**
 * 监听屏幕大小有针对性的加载功能
 */
        window.addEventListener("resize", function(event) {
            if (document.body.clientWidth <= 760 && window.resume.mode != "mobile") {
                mobileInit();
            } else if (document.body.clientWidth > 760 && window.resume.mode == "mobile") {
                pcInit();
            }
        });

        function listInit() {
            var application;
            if (self.app) {
                application = new app();
            }
            for (once = 0; once < navLi.length; once++) {
                removeClass(navLi[once], "active");
                var nc = navLi[once].childNodes;
                if (nc[0].attributes['href'].value == location.hash) {
                    if (window.resume.mode == "mobile") {
                        ctrlMobile("hide");
                    }
                    navLi[once].className += " active";
                    if (self.app) {
                        var swap = location.hash.substr(1,location.hash.length-1);
                        striped(30);
                        application[swap]();
                    }
                }
            }
        }listInit();
        window.addEventListener("hashchange", function(event) {
            listInit()
        }, false);
        if (location.hash == "" || location.hash == "#") {
            location.href = "#"+ window.resume.home;
        }

    });
})();